﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Diagnostics;

namespace PositionalIndexer
{
    class Program
    {
        static void Main(string[] args)
        {
            //String INPUT_FILE_PATH = "08.warc";
            String OUTPUT_FILE_PATH = "PositionalIndexer.rtf";
            DocumentManager documentManager = new DocumentManager();
            Tokenizer tokenizer = new Tokenizer();
            Indexer indexer = new Indexer();

            Console.Write("Input file name >>");
            String INPUT_FILE_PATH = Console.ReadLine();

            Console.WriteLine("Start to read the file...");
            StreamReader sr = WARCFile.ReadFile(INPUT_FILE_PATH);
            documentManager.ParseDocument(sr);
            sr.Close();
            Console.WriteLine("Read file done.");
            Console.WriteLine("Total: " + documentManager.DocumentDictionary.Count + " documents.\n");

            Console.WriteLine("Start to find tokens...");
            foreach (KeyValuePair<int, string>document in documentManager.DocumentDictionary)
            {
                tokenizer.Tokenize(document.Key, document.Value);
            }
            Console.WriteLine(tokenizer.TokenDictionary.Count + " tokens be find.\n");

            Console.WriteLine("Start to generate positional index...");
            indexer.GenerateIndex(tokenizer.TokenDictionary);
            Console.WriteLine("Positional index done.");
            Console.WriteLine("Total: " + indexer.IndexDictionary.Count + " terms.\n");

            Console.WriteLine("Start to write the positional indexer file...");
            StringBuilder text = indexer.FormatIndex(indexer);
            WARCFile.WriteFile(OUTPUT_FILE_PATH, text.ToString());
            Console.WriteLine("Positional indexer file is done!!!");

            // pause system
            Console.ReadKey();
        }
    }
}